package org.jeecg.glr.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SysUtil;
import org.jeecg.glr.domain.glr.*;
import org.jeecg.glr.domain.led.DataDomain;
import org.jeecg.glr.domain.glr.GlrGateIlluminanceDomain;
import org.jeecg.glr.entity.GlrGateway;
import org.jeecg.glr.entity.GlrPattern;
import org.jeecg.glr.entity.GlrPatternGateway;
import org.jeecg.glr.iframe.controller.GlrIframeLightGis;
import org.jeecg.glr.mapper.GlrGatewayMapper;
import org.jeecg.glr.service.IGateWayCommon;
import org.jeecg.glr.util.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Service
public class GateWayCommon extends ServiceImpl<GlrGatewayMapper, GlrGateway> implements IGateWayCommon {

    @Value("${glr.url.light_gateway_read_date_time}")
    private String light_gateway_read_date_time;

    @Value("${glr.url.light_gateway_set_date_time}")
    private String light_gateway_set_date_time;
    @Value("${glr.url.light_gateway_light}")
    private String light_gateway_light;

    @Value("${glr.url.light_gate_way_set_light_gateway}")
    private String light_gate_way_set_light_gateway;

    @Value("${glr.url.light_gate_way_get_open_status}")
    private String light_gate_way_get_open_status;
    @Value("${glr.url.get_token}")
    private String get_token;

    @Value("${glr.url.light_gate_way_set_open_status}")
    private String light_gate_way_set_open_status;

    @Value("${glr.url.light_gate_way_get_ip}")
    private String light_gate_way_get_ip;

    @Value("${glr.url.light_gate_way_set_ip}")
    private String light_gate_way_set_ip;

    @Value("${glr.url.light_gate_way_get_msg}")
    private String light_gate_way_get_msg;

    @Value("${glr.url.light_gate_way_set_msg}")
    private String light_gate_way_set_msg;
    @Value("${glr.url.light_gate_way_set_packet_loss}")
    private String light_gate_way_set_packet_loss;

    @Value("${glr.url.light_gate_way_get_packet_loss}")
    private String light_gate_way_get_packet_loss;

    @Value("${glr.url.light_gate_way_get_gps}")
    private String light_gate_way_get_gps;

    @Value("${glr.url.light_gate_way_set_gps}")
    private String light_gate_way_set_gps;

    @Value("${glr.url.light_gate_way_get_powerandtime}")
    private String light_gate_way_get_powerandtime;

    @Value("${glr.url.light_gateway_status}")
    private String light_gateway_status;

    @Value("${glr.url.light_gateway_control_setonoff}")
    private String light_gateway_control_setonoff;
    @Value("${glr.url.light_gateway_get_light_date}")
    private String light_gateway_get_light_date;
    @Value("${glr.url.light_gateway_set_timefram}")
    private String light_gateway_set_timefram;
    @Value("${glr.url.light_gateway_get_data}")
    private String light_gateway_get_data;

    @Value("${glr.url.light_gate_way_set_illumination}")
    private String light_gate_way_set_illumination;

    @Value("${glr.url.light_gate_way_get_illumination}")
    private String light_gate_way_get_illumination;

    @Value("${glr.url.light_gate_way_set_manufacturer_set}")
    private String light_gate_way_set_manufacturer_set;

    @Value("${glr.url.light_gate_way_get_manufacturer_set}")
    private String light_gate_way_get_manufacturer_set;

    @Value("${glr.url.light_gate_way_set_rated_power}")
    private String light_gate_way_set_rated_power;

    @Value("${glr.url.light_gate_way_get_rated_power}")
    private String light_gate_way_get_rated_power;

    @Value("${glr.url.light_gate_way_set_phase}")
    private String light_gate_way_set_phase;

    @Value("${glr.url.light_gate_way_get_send_command}")
    private String light_gate_way_get_send_command;

    @Value("${glr.url.light_gate_way_set_firmware_update}")
    private String light_gate_way_set_firmware_update;

    @Value("${glr.url.light_gate_way_get_address}")
    private String light_gate_way_get_address;

    @Value("${glr.url.light_gate_way_set_address}")
    private String light_gate_way_set_address;

    @Value("${glr.url.light_gate_way_get_version_no}")
    private String light_gate_way_get_version_no;

    @Value("${glr.url.light_gate_way_set_system_restart}")
    private String light_gate_way_set_system_restart;

    @Value("${glr.url.light_gate_way_set_sub_control_m_a_c}")
    private String light_gate_way_set_sub_control_m_a_c;

    @Value("${glr.url.light_gate_way_get_sub_control_m_a_c}")
    private String light_gate_way_get_sub_control_m_a_c;

    @Resource
    RedisUtil redisUtil;


    @Override
    public Boolean ledOpenOrClose(GlrGateWayControlLight glrGateWayControlLight) {

        String  gateWayCodeStart=glrGateWayControlLight.getGateWayCode().substring(0,4);
         /*
        0101
        0301
        写控制分控07
        00 单控 01 多控 02 组控 03 全控
        */
        if ("0101".equals(gateWayCodeStart)||"0301".equals(gateWayCodeStart)){

        }
        /*
        0501
        写控制分控 07
        00 全控 01 多控
         */
        if ("0501".equals(gateWayCodeStart)||"0601".equals(gateWayCodeStart)){
            String  type=glrGateWayControlLight.getType();
            if ("03".equals(type)){
                type="00";
            }else{
                type="01";
            }
            glrGateWayControlLight.setType(type);
        }


        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gateway_control_setonoff, HttpRequestUtil.POST, glrGateWayControlLight,getToken());
        if (json.isOk()) {
                return true;
        }
        return false;
    }

    @Override
    public ResponseMessage<DataDomain> getGateWayStatus(String gateWayCode) {
        String url = light_gateway_status.replaceAll("GATEWAY_CODE", gateWayCode);
        return httpRequestResultAjaxJsonDataDoamin(url, HttpRequestUtil.POST, null,getToken());
    }
    @Override
    public ResponseMessage readDate(DateTimeDomain dateTimeDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gateway_read_date_time, HttpRequestUtil.POST, dateTimeDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(dateTimeDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_DATE_TIME);
                if (obj != null) {
                    ResponseMessage domain =JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean setDate(DateTimeDomain dateTimeDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gateway_set_date_time, HttpRequestUtil.POST, dateTimeDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(dateTimeDomain.getGateWayCode() + CMDCommand.SET_LED_MSG_DATE_TIME);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    @Override
    public ResponseMessage readLightGateWay(GataWayDomain gataWayDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gateway_light, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_LIGHT_GATAWAY);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean readGateWay(GataWayDomain gataWayDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gateway_get_data, HttpRequestUtil.POST, gataWayDomain,getToken());
        return json.isOk();
    }

    @Override
    public Boolean setLightGateWay(GlrGateWayLightGateWayDomain glrGateWayLightGateWayDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_light_gateway, HttpRequestUtil.POST, glrGateWayLightGateWayDomain,getToken());
        if (json.isOk()) {
           /* try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gateWayCode + CMDCommand.SET_LED_MSG_DATE_TIME);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }*/
           return true;
        }

        return false;
    }

    @Override
    public ResponseMessage getOpenStatus(GataWayDomain gataWayDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_open_status, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_POWER_OPEN_CLOSE);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean setOpenStatus(GlrGateWayOpenSatusDomain glrGateWayOpenSatusDomain) {
         httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_open_status, HttpRequestUtil.POST, glrGateWayOpenSatusDomain,getToken());
         return true;
       /* ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_open_status, HttpRequestUtil.POST, glrGateWayOpenSatusDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(glrGateWayOpenSatusDomain.getGateWayCode() + CMDCommand.SET_LED_MSG_DATE_TIME);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }

        return false;*/
    }

    @Override
    public ResponseMessage getIP(GlrIPDomain ipdomain) {

        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_ip, HttpRequestUtil.POST, ipdomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(ipdomain.getGateWayCode() + CMDCommand.GET_LED_MSG_IP);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean setIP(GlrIPDomain ipdomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_ip, HttpRequestUtil.POST, ipdomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(ipdomain.getGateWayCode() + CMDCommand.SET_LED_MSG_DATE_TIME);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }

        return false;
    }

    @Override
    public ResponseMessage getGateWayMsg(GataWayDomain gataWayDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_msg, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_SYS_DATE);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean setGateWayMsg(GlrGateWayMsgDomain glrGateWayMsgDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_msg, HttpRequestUtil.POST, glrGateWayMsgDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(glrGateWayMsgDomain.getGateWayCode() + CMDCommand.SET_LED_MSG_SYS_DATE);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }

        return false;
    }

    @Override
    public Boolean setPacketLoss(GlrGateWayTestDomain glrGateWayTestDomain)
    {

        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_packet_loss, HttpRequestUtil.POST, glrGateWayTestDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(glrGateWayTestDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_PACKET_LOSS);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }

        return false;
    }

    @Override
    public ResponseMessage getPacketLoss(GataWayDomain gataWayDomain) {
        Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_PACKET_LOSS);
        if (obj != null) {
            ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
            return domain;
        }
        return null;
    }

    @Override
    public ResponseMessage getGPS(GlrGateWayGpsDomain gataWayDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_gps, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                int GET_GPS=0;
                String stgateWayCode1=gataWayDomain.getGateWayCode().substring(0,4);
                if ("0101".equals(stgateWayCode1)){
                    GET_GPS=CMDCommand.GET_LED_GPS;
                }else {
                    GET_GPS=CMDCommand.GET_LED_GPS1;
                }

                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + GET_GPS);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean setGPS(GlrGateWayGpsDomain glrGateWayGpsDomain){
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_gps, HttpRequestUtil.POST, glrGateWayGpsDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(glrGateWayGpsDomain.getGateWayCode() + CMDCommand.GET_LED_GPS);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    @Override
    public ResponseMessage getTimeFrameAndDimming(GataWayDomain common) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_powerandtime, HttpRequestUtil.POST, common,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);

                int GET_MSG_VALUE=0;
                String stgateWayCode1=common.getGateWayCode().substring(0,4);
                if ("0101".equals(stgateWayCode1)||"0301".equals(stgateWayCode1)){
                    GET_MSG_VALUE=CMDCommand.GET_LED_MSG_TIME_FRAME;
                }else {
                    GET_MSG_VALUE=CMDCommand.GET_LED_MSG_TIME_FRAME1;
                }

                Object obj = redisUtil.get(common.getGateWayCode() + GET_MSG_VALUE);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean readLightData(String gateWayCode) {
        String url=light_gateway_get_light_date.replaceAll("GATEWAY_CODE", gateWayCode);
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(url, HttpRequestUtil.POST, null,getToken());
        return json.isOk();
    }

    @Override
    public Boolean setTimeFrameAndDimming(GlrGateWayPatternDomain glrGateWayPatternDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gateway_set_timefram, HttpRequestUtil.POST, glrGateWayPatternDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);

                int GET_MSG_VALUE=0;
                String stgateWayCode1=glrGateWayPatternDomain.getGateWayCode().substring(0,4);
                if ("0101".equals(stgateWayCode1)||"0301".equals(stgateWayCode1)){
                    GET_MSG_VALUE=CMDCommand.SET_LED_MSG_TIME_FRAME;
                }else {
                    GET_MSG_VALUE=CMDCommand.SET_LED_MSG_TIME_FRAME1;
                }

                Object obj = redisUtil.get(glrGateWayPatternDomain.getGateWayCode() + GET_MSG_VALUE);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    @Override
    public Boolean setTimeFrameAndDimming(GlrPatternGateway glrPatternGateway) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gateway_set_timefram, HttpRequestUtil.POST, glrPatternGateway,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(glrPatternGateway.getGateWayCode() + CMDCommand.SET_LED_MSG_TIME_FRAME);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    @Override
    public Boolean setTimeFrameAndDimming(GlrPattern glrPattern) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gateway_set_timefram, HttpRequestUtil.POST, glrPattern,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(glrPattern.getGateWayCode() + CMDCommand.SET_LED_MSG_TIME_FRAME);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    @Override
    public Boolean setIlluminance(GlrGateIlluminanceDomain glrGateIlluminanceDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_illumination, HttpRequestUtil.POST, glrGateIlluminanceDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(glrGateIlluminanceDomain.getGateWayCode() + CMDCommand.SET_LED_ILLUMINATION1);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    @Override
    public ResponseMessage getIlluminance(GataWayDomain gataWayDomain) {
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_illumination, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_ILLUMINATION);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean setManufacturerSet(GlrGateWayManufacturerSetDomain glrGateWayManufacturerSetDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_manufacturer_set, HttpRequestUtil.POST, glrGateWayManufacturerSetDomain,getToken());
        return json.isOk();
    }

    @Override
    public ResponseMessage getManufacturerSet(GataWayDomain gataWayDomain) {
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_manufacturer_set, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_MANUFACTURER);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }


    @Override
    public Boolean setRatedPower(GlrGateWayRatedPowerDomain glrGateWayRatedPowerDomain) {
        ResponseMessage json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_rated_power, HttpRequestUtil.POST, glrGateWayRatedPowerDomain,getToken());
        return json.isOk();
    }

    @Override
    public ResponseMessage getRatedPower(GataWayDomain gataWayDomain) {
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_rated_power, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_ILLUMINATION);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public ResponseMessage readLightAddress(GataWayDomain gataWayDomain) {
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_address, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_SECOND_CONTROL);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean setLightAddress(GlrLightGateAddressDomain glrLightGateAddressDomain) {
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_address, HttpRequestUtil.POST, glrLightGateAddressDomain,getToken());
        return json.isOk();
    }


    @Override
    public ResponseMessage readLightPhase(GataWayDomain gataWayDomain){
        gataWayDomain.setCmd(CMDCommand.GET_LED_MSG_PHASE);
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_send_command, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_PHASE);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public Boolean setLightPhase(GlrLightGatePhaseDomain glrLightGatePhaseDomain) {
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_phase, HttpRequestUtil.POST, glrLightGatePhaseDomain,getToken());
        return json.isOk();
    }

    @Override
    public ResponseMessage getVersionNo(GataWayDomain gataWayDomain){
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_version_no, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_VERSION_NO);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }


    @Override
    public ResponseMessage readLightRatedPower(GataWayDomain gataWayDomain){
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_rated_power, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_RATED_POWER);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }


    @Override
    public Boolean setLightRatedPower(GlrLightGateRatedPowerDomain glrLightGateRatedPowerDomain){
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_rated_power, HttpRequestUtil.POST, glrLightGateRatedPowerDomain,getToken());
        return json.isOk();
    }

    @Override
    public Boolean setFirmwareUpdate(GlrLightGateFirmwareUpdateDomain glrLightGateFirmwareUpdateDomain){
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_firmware_update, HttpRequestUtil.POST, glrLightGateFirmwareUpdateDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(5000);
                Object obj = redisUtil.get(glrLightGateFirmwareUpdateDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_FIRMWARE_UPDATE);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    if (domain.getRespCode().equals(GLRConstant.GLR_GATE_WAY_RESULT_VALUE_0)) {
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }


    @Override
    public Boolean setSubControlMAC(GlrGateWaySubControlMACDomain glrGateWaySubControlMACDomain){
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_sub_control_m_a_c, HttpRequestUtil.POST, glrGateWaySubControlMACDomain,getToken());
        if (json.isOk()) {
            return true;
        }
        return null;
    }


    @Override
    public ResponseMessage getSubControlMAC(GataWayDomain gataWayDomain){
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_get_sub_control_m_a_c, HttpRequestUtil.POST, gataWayDomain,getToken());
        if (json.isOk()) {
            try {
                Thread.sleep(GLRConstant.GLR_GATE_WAY_SLEEP_TIME);
                Object obj = redisUtil.get(gataWayDomain.getGateWayCode() + CMDCommand.GET_LED_MSG_VERSION_NO);
                if (obj != null) {
                    ResponseMessage domain =  JSONUtil.getResponseMessage(obj.toString());
                    return domain;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }




    @Override
    public Boolean setSystemRestart(GlrGateWaySystemRestartDomain glrGateWaySystemRestartDomain) {
        ResponseMessage<DataDomain> json = httpRequestResultAjaxJsonDataDoamin(light_gate_way_set_system_restart, HttpRequestUtil.POST, glrGateWaySystemRestartDomain,getToken());
        return json.isOk();
    }


    /**
     * 发起https请求并获取结果
     *
     * @param requestUrl
     *            请求地址
     * @param requestMethod
     *            请求方式（GET、POST）
     * @param outputStr
     *            提交的数据
     * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
     */
    private static ResponseMessage<DataDomain> httpRequestResultAjaxJsonDataDoamin(String requestUrl, String requestMethod,
                                                                                  Object outputStr,String token)
    {
        ResponseMessage<DataDomain> msg = new ResponseMessage<DataDomain>();
        StringBuffer buffer = new StringBuffer();
        try
        {
            // 建立连接
            URL url = new URL(requestUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod(requestMethod);
            connection.setRequestProperty(CommonConstant.St_Access_Token, token);
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            // 传输的类型
            if("GET".equalsIgnoreCase(requestMethod))
            {
                connection.connect();
            }

            // 当有数据需要提交时
            if(null != outputStr)
            {
                OutputStream outputStream = connection.getOutputStream();
                String dataValue=  JSONObject.toJSONString(outputStr);
                // 注意编码格式，防止中文乱码
                outputStream.write(dataValue.getBytes("UTF-8"));
                outputStream.close();
            }
            // 流处理
            InputStream input = connection.getInputStream();
            InputStreamReader inputReader = new InputStreamReader(input, "UTF-8");
            BufferedReader reader = new BufferedReader(inputReader);
            String line;
            while ((line = reader.readLine()) != null)
            {
                buffer.append(line);
            }
            // 关闭连接、释放资源
            reader.close();
            inputReader.close();
            input.close();
            connection.disconnect();
            ObjectMapper objectMapper = new ObjectMapper();
            if(buffer.length()>0){
                msg = objectMapper.readValue(buffer.toString(), ResponseMessage.class);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return msg;
    }

    @Override
    public String getToken() {
        LoginUser user=SysUtil.getUserInfo();
        if (user==null){
            user= GlrIframeLightGis.loginUserTemp;
        }
        String token= null;
        if(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + user.getUsername())!=null){
            token=redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + user.getUsername()).toString();
            redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + user.getUsername(), token,CommonConstant.TOKEN_EXPIRE_TIME*2);
        }else{
            Map<String,String> map = new HashMap<>();
            map.put("username", user.getUsername());
            map.put("password", user.getPassword());
            JSONObject json=HttpRequestUtil.doPost(get_token,map,"UTF-8");
            if(json!=null){
                token=json.get("data").toString();
            }
            redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + user.getUsername(), token,CommonConstant.TOKEN_EXPIRE_TIME*2);
        }

        return token;
    }



}
